﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class CategoryController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<Categories> category = (from c in OTDb.Categories
                                         select new Categories
                                         {
                                             CategoryID = c.CategoryID,
                                             CategoryName = c.CategoryName,
                                             Description = c.Description,
                                             SubCategoryID = c.SubCategoryID,
                                             CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                             UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue,
                                             LanguageID = c.LanguageID,
                                             Status = c.Status
                                         }).OrderBy(o => o.CategoryName).ToList<Categories>();

            if (!string.IsNullOrEmpty(searchString))
                category = category.Where(r => r.CategoryName.ToLower().Contains(searchString.ToLower())).ToList();
            int recount =category.Count;
            category = category.Skip<Categories>(pgindex * (pageNumber - 1))
                .Take<Categories>(pgindex).ToList<Categories>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = category,
                RecordCount = recount
            });
        }
        #endregion

        #region Create
        public ActionResult Create()
        {
            Categories Category = null;
            try
            {
                Category = new Categories();
                var Categorys = OTDb.Categories.Where(c=>c.Status==true).ToList();
                Category.SubCategories = Categorys;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Category);
        }
        [HttpPost]
        public string Create( string CategoryName, string Description,Boolean status,int subcategory=0)
        {
            string result = "";
            Category Category = null;
            try
            {
                if (ModelState.IsValid)
                {
                    Category = new Category();
                    Category.CategoryName = CategoryName;
                    Category.Description = Description;
                    Category.Status = status;
                    Category.LanguageID = LanguageID;
                    Category.SubCategoryID = subcategory;
                    Category.CreatedDate = DateTime.Now;
                    Category.UpdatedDate = DateTime.Now;
                    OTDb.Categories.Add(Category);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.States.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int CategoryID)
        {
            Categories categories = null;
            try
            {
                var categry = OTDb.Categories.Where(r => r.CategoryID == CategoryID).SingleOrDefault();
                if (categry != null)
                {
                    categories = new Categories();
                    categories.CategoryID = categry.CategoryID;
                    categories.CategoryName = categry.CategoryName;
                    categories.Description = categry.Description;
                    categories.Status = categry.Status;
                    categories.LanguageID = LanguageID;
                    var Categorys = OTDb.Categories.Where(c=>c.Status==true).ToList();
                    categories.SubCategories = Categorys;
                    if (categry.SubCategoryID != null)
                        categories.SubCategoryID = (int)categry.SubCategoryID;
                    var category = OTDb.Categories.Where(r => r.LanguageID == LanguageID).ToList();
                  }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(categories);
        }
        [HttpPost]
        public string Edit(int CategoryID, string CategoryName, string Description, Boolean Status, int subcategory=0)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var Category = OTDb.Categories.Where(r => r.CategoryID == CategoryID).SingleOrDefault();
                    if (Category != null)
                    {
                        Category.CategoryID = CategoryID;
                        Category.CategoryName = CategoryName;
                        Category.Description = Description;
                        Category.Status = Status;
                        Category.LanguageID = LanguageID;
                        Category.UpdatedDate = DateTime.Now;
                        Category.SubCategoryID = subcategory;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                       result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int CategoryID)
        {
            Categories categories = null; 
            try
            {
                var categry = OTDb.Categories.Where(r => r.CategoryID == CategoryID).SingleOrDefault();
                if (categry != null)
                {
                    categories = new Categories();
                    categories.CategoryName = categry.CategoryName;
                    categories.CategoryID = categry.CategoryID;
                    categories.Description = categry.Description;
                    categories.LanguageID = categry.LanguageID;
                    categories.Status = categry.Status;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(categories);
        }
        [HttpPost]
        public string Delete(int CategoryID, string IsDelete)
        {
            string result = "";
            try
            {
                var cat = OTDb.Categories.Where(r => r.CategoryID == CategoryID).SingleOrDefault();
                if (cat != null)
                {
                    OTDb.Categories.Remove(cat);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}